package pri.zey.custom.tools;/**
 * Created by Administrator on 2016/4/29 0029.
 */

import com.alibaba.fastjson.JSON;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>类描述
 * >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>属性描述
 * >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>方法描述
 */
public class Tto {

    private String TGA = "Tto";

    private Tto instance;

    private Tto(){}

    private Tto getInstance(){
        if(instance == null){
            instance = new Tto();
        }
        return instance;
    }

    public static int StrToInt(String para){
        if(Tkit.notBlank(para) && Tkit.isNumber(para)){
           return Integer.valueOf(para);
        }
        return -1;
    }

    public static String IntToStr(int para){
        return String.valueOf(para);
    }

    public static Map StrToMap(String para){
        Map map = new HashMap();
        try {
            if(para != null && para.trim().length() > 0) {
                JSONObject json = new JSONObject(para);
                Iterator keys = json.keys();
                while (keys.hasNext()) {
                    String key = (String) keys.next();
                    String value = json.get(key).toString();
                    if (value.startsWith("{") && value.endsWith("}")) {
                        map.put(key, StrToMap(value));
                    } else {
                        map.put(key, value);
                    }

                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return map;
    }

    public static <T>T StrToT(String para, Class cls){
        return (T) JSON.parseObject(para, cls);
    }

    public static <T>T StrToList(String para, Class cls){
        return (T) JSON.parseArray(para, cls);
    }
}
